package kr.saeildamil.androidstudty.ch12;

import java.util.ArrayList;

import kr.saeildamil.androidstudty.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MyListAdapter extends BaseAdapter {
	Context mainContext;
	int icontextLayout;
	ArrayList<MyItem> arSrc;
	LayoutInflater Inflater;
	
	
	
	
	
	public MyListAdapter(Context mainContext, int icontextLayout,
			ArrayList<MyItem> arSrc) {
		super();
		this.mainContext = mainContext;
		this.icontextLayout = icontextLayout;
		this.arSrc = arSrc;
		this.Inflater = (LayoutInflater)mainContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		
		return arSrc.size();
	}

	@Override
	public Object getItem(int position) {
		
		return arSrc.get(position).name;
	}

	@Override
	public long getItemId(int position) {
		
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final int pos = position;
		if (convertView == null){
			
			convertView = Inflater.inflate(icontextLayout, parent, false);
		}
		ImageView img = (ImageView)convertView.findViewById(R.id.ch12_icontext_img);
		img.setImageResource(arSrc.get(position).iCon);
		
		TextView tv =(TextView)convertView.findViewById(R.id.ch12_icontext_tv);
		tv.setText(arSrc.get(position).name);
		
		Button btn =(Button) convertView.findViewById(R.id.ch12_icontext_btn);
				btn.setOnClickListener(new Button.OnClickListener(){

					public void onClick(View v) {
						String str = getItem(pos) + "주문 합니다.";
						Toast.makeText(mainContext, str, Toast.LENGTH_LONG).show();
					}
				});
				
				return convertView;
	}

}
